package com.zx.springboot.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/***
 * Rabbitmq的springboot配置
 * @author Administrator
 *
 */
@Configuration
public class RabbitmqConfig {
	
	/***
	 * 直连消息队列 key名称
	 */
	public static final String QUEUE = "queue_id_hello"; 

	/**
	 * Direct模式 消息队列
	 * @return
	 */
	@Bean
	public Queue directQueue() {
		// 第一个参数是队列名字， 第二个参数是指是否持久化
		return new Queue(QUEUE, true);
	}
	
	//定义4个消息队列名称
	public static final String QUEUQ_1 = "queue_1";
	public static final String QUEUQ_2 = "queue_2";
	public static final String QUEUQ_3 = "queue_3";
	public static final String QUEUQ_4 = "queue_4";
	//创建4个消息队列
	@Bean("queue1")
	public Queue getQueue1() {return new Queue(QUEUQ_1);}
	@Bean("queue2")
	public Queue getQueue2() {return new Queue(QUEUQ_2);}
	@Bean("queue3")
	public Queue getQueue3() {return new Queue(QUEUQ_3);}
	@Bean("queue4")
	public Queue getQueue4() {return new Queue(QUEUQ_4);}
	//定义3个交换机名称
	public static final String EXCHANGE_DIRECT = "exchange_direct";
	public static final String EXCHANGE_TOPIC = "exchange_topic";
	public static final String EXCHANGE_FANOUT = "exchange_fanout";
	//创建3个交换机
	@Bean("directExchange")
	public DirectExchange getDirectExchange() {return new DirectExchange(EXCHANGE_DIRECT);}
	@Bean("topicExchange")
	public TopicExchange getTopicExchange() {return new TopicExchange(EXCHANGE_TOPIC);}
	@Bean("fanoutExchange")
	public FanoutExchange getFanoutExchange() {return new FanoutExchange(EXCHANGE_FANOUT);}
	
	public static final String DIRECT_KEY = "*hello.test*";
	public static final String TOPIC_KEY = "*.hello.*";
	//定义4个绑定关系
	@Bean
	public Binding bindFirst(@Qualifier("queue1") Queue queue,
			@Qualifier("directExchange") DirectExchange directExchange) {
		return BindingBuilder.bind(queue).to(directExchange).with(DIRECT_KEY);
	}
	@Bean
	public Binding bindSecond(@Qualifier("queue2") Queue queue,
			@Qualifier("topicExchange") TopicExchange topicExchange) {
		return BindingBuilder.bind(queue).to(topicExchange).with(TOPIC_KEY);
	}
	@Bean
	public Binding bindThird(@Qualifier("queue3") Queue queue,
			@Qualifier("fanoutExchange") FanoutExchange fanoutExchange) {
		return BindingBuilder.bind(queue).to(fanoutExchange);
	}
	@Bean
	public Binding bindForth(@Qualifier("queue4") Queue queue,
			@Qualifier("fanoutExchange") FanoutExchange fanoutExchange) {
		return BindingBuilder.bind(queue).to(fanoutExchange);
	}
	/***
	 * JSON格式传输
	 * @return
	 */
	@Bean
    public Jackson2JsonMessageConverter producerJackson2MessageConverter() {
        return new Jackson2JsonMessageConverter();
    }
}
